import regeneratorRuntime from '../../lib/runtime/runtime';
import {request} from "../../request/index.js";



// pages/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    isFocus:false,
    inpValue:''
  },
  TimeId : -1,
  //输入框
  handleInput(e){
    //获取输入框的值
    const {value} = e.detail;
    //输入合法性
    if(!value.trim()){
      //值不合法
      this.setData({
        goods:[],
        isFocus:false
      })
      return;
    }
    this.setData({
      isFocus:true
    })
    //发送请求获取数据
    clearTimeout(this.TimeId);
    this.TimeId = setTimeout(() => {
      this.qsearch(value);
    },1000)
    

  },
//发送请求获取数据
  async qsearch(query){
    const res = await request({url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch",data:{query}});
    // console.log(res.data.message);
    this.setData({
      goods:res.data.message
    })
  },

  //点击取消按钮
  handleCancel(){
    this.setData({
      inpValue:'',
      isFocus:false,
      goods:[]
    })
  }

})